/**
 * 
 */
package org.t2.game.server.module.data.service.parser;

import java.util.List;
import java.util.Map;

import org.gaming.tool.DateTimeUtil;
import org.gaming.tool.GsonUtil;
import org.gaming.tool.ListMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.t2.design.module.CheckTable;
import org.t2.design.module.autogen.CheckAbstractTable.CheckCfg;
import org.t2.game.server.module.bag.model.Bag;
import org.t2.game.server.module.bag.model.CheatLog;
import org.t2.game.server.module.bag.service.BagService;
import org.t2.game.server.module.data.model.Data;
import org.t2.game.server.module.data.saver.PlayerDiamondSaver;
import org.t2.game.server.module.data.saver.PlayerFeatherSaver;
import org.t2.game.server.module.data.saver.PlayerGoldSaver;
import org.t2.game.server.module.data.saver.PlayerMuscleSaver;
import org.t2.game.server.module.data.saver.PlayerPointSaver;
import org.t2.game.server.module.data.service.DataParserService;
import org.t2.game.server.module.data.struct.BagData;
import org.t2.game.server.module.data.struct.BagItem;
import org.t2.game.server.module.player.model.Player;
import org.t2.game.server.module.player.service.PlayerService;

/**
 * 
 */
@Component
public class BagParserService extends DataParserService {

	@Autowired
	private BagService bagService;
	@Autowired
	private PlayerGoldSaver playerGoldSaver;
	@Autowired
	private PlayerDiamondSaver playerDiamondSaver;
	@Autowired
	private PlayerMuscleSaver playerMuscleSaver;
	@Autowired
	private PlayerPointSaver playerPointSaver;
	@Autowired
	private PlayerFeatherSaver playerFeatherSaver;
	@Autowired
	private PlayerService playerService;
	
	@Override
	public String dataType() {
		return "bag";
	}

	@Override
	public void parser(long playerId, List<Data> datas, String reason) {
		Player player = playerService.getEntity(playerId);
		Bag bag = bagService.getEntity(playerId);
		
		boolean needUpdate = false;
		
		for(Data data : datas) {
			BagData bagData = GsonUtil.parseJson(data.getPlaintext(), BagData.class);
			if(bagData.getBagData() == null) {
				continue;
			}
			Map<Integer, BagItem> itemMap = ListMapUtil.listToMap(bagData.getBagData(), BagItem::getId);
			//金币
			BagItem goldItem = itemMap.get(101001);
			if(goldItem != null && bag.getGold() != goldItem.getNum()) {
				playerGoldSaver.setNumber(bag, reason, goldItem.getNum());
				needUpdate = true;
			}
			//钻石
			BagItem diamondItem = itemMap.get(101002);
			if(diamondItem != null && bag.getGold() != diamondItem.getNum()) {
				playerDiamondSaver.setNumber(bag, reason, diamondItem.getNum());
				needUpdate = true;
			}
			//体力
			BagItem muscleItem = itemMap.get(102001);
			if(muscleItem != null && bag.getMuscle() != muscleItem.getNum()) {
				playerMuscleSaver.setNumber(bag, reason, muscleItem.getNum());
				needUpdate = true;
			}
			//积分
			BagItem pointItem = itemMap.get(102013);
			if(pointItem != null && bag.getPoint() != pointItem.getNum()) {
				playerPointSaver.setNumber(bag, reason, pointItem.getNum());
				needUpdate = true;
			}
			//鹅毛
			BagItem featherItem = itemMap.get(102015);
			if(featherItem != null && bag.getFeather() != featherItem.getNum()) {
				playerFeatherSaver.setNumber(bag, reason, featherItem.getNum());
				needUpdate = true;
			}
			//
			for(BagItem bagItem : itemMap.values()) {
				long oldNum = bag.getItems().getOrDefault(bagItem.getId(), 0L);
				long newNum = bagItem.getNum();
				long delta = newNum - oldNum;
				CheckCfg checkCfg = CheckTable.ins().findInIdIndex(bagItem.getId());
				if(checkCfg == null) {
					continue;
				}
				if(delta > 0 && (delta > checkCfg.getGetNum() || newNum > checkCfg.getTotalNum())) {
					//检测异常
					CheatLog cheatLog = new CheatLog();
					cheatLog.setPlayerId(player.getId());
					cheatLog.setNick(player.getNick());
					cheatLog.setItemId(checkCfg.getId());
					cheatLog.setItemName(checkCfg.getName());
					cheatLog.setDelta(delta);
					cheatLog.setDeltaCheat(delta > checkCfg.getGetNum());
					cheatLog.setCurrNum(newNum);
					cheatLog.setCurrCheat(newNum > checkCfg.getTotalNum());
					cheatLog.setTime(DateTimeUtil.currMillis());
					cheatLog.setReason(reason);
					bagService.addCheat(cheatLog);
				}
				
				
				bag.getItems().put(bagItem.getId(), newNum);
			}
		}
		if(needUpdate) {
			bagService.update(bag);
		}
	}

}
